unit F_BuscarMaterias;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, ExtCtrls, StdCtrls, Grids,
  DBGrids, Buttons, ZAbstractRODataset, ZDataset;

type
  TF_SeleccionarMateria = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    DBG_Categoria: TDBGrid;
    DS_Categoria: TDataSource;
    DBG_Test: TDBGrid;
    DS_Test: TDataSource;
    Panel4: TPanel;
    Label1: TLabel;
    E_Busqueda: TEdit;
    BB_Buscar: TBitBtn;
    BB_Cancelar: TBitBtn;
    BB_Seleccionar: TBitBtn;
    Q_Carrera: TZReadOnlyQuery;
    Q_Materia: TZReadOnlyQuery;
    Splitter: TSplitter;
    Q_CarreraID: TIntegerField;
    Q_CarreraNOMBRE: TStringField;
    Q_CarreraAVREVIATURA: TStringField;
    Q_MateriaID: TIntegerField;
    Q_MateriaDENOMINACION: TStringField;
    Q_MateriaCODIGO: TIntegerField;
    Q_MateriaFK_IDMATERIA: TIntegerField;
    Q_MateriaFK_IDCARRERA: TIntegerField;
    Q_MateriaANIO: TIntegerField;
    Q_MateriaCUATRIMESTRE: TIntegerField;
    procedure B_BusquedaClick(Sender: TObject);
    procedure B_SeleccionarClick(Sender: TObject);
    procedure B_CancelarClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure DBG_TestKeyPress(Sender: TObject; var Key: Char);
    procedure E_BusquedaKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormResize(Sender: TObject);
  private
    { Private declarations }
    procedure BuscarTest;
    procedure AjustarVentana;
    procedure FixDBGridColumnsWidth(const DBGrid: TDBGrid);
  public
    { Public declarations }
  end;

implementation

uses DM_Persistencia;

{$R *.dfm}
procedure TF_SeleccionarMateria.BuscarTest;
var
  s:string;
begin
  s:='SELECT * FROM MATERIAS JOIN MATERIAS_POR_CARRERA ON MATERIAS.ID = MATERIAS_POR_CARRERA.FK_IDMATERIA WHERE MATERIAS_POR_CARRERA.FK_IDCARRERA= :ID AND DENOMINACION CONTAINING '+QuotedStr(E_Busqueda.Text)+' ORDER BY DENOMINACION';
  with Q_Materia do
    begin
      Close;
      SQL.Clear;
      SQL.Add(s);
      Open;
    end;
end;
procedure TF_SeleccionarMateria.B_BusquedaClick(Sender: TObject);
begin
  BuscarTest();
end;

procedure TF_SeleccionarMateria.B_SeleccionarClick(Sender: TObject);
begin
  ModalResult:=mrOk;
end;

procedure TF_SeleccionarMateria.B_CancelarClick(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

procedure TF_SeleccionarMateria.FormCreate(Sender: TObject);
begin
  Q_Carrera.Active:=true;
  Q_Materia.Active:=true;
end;

procedure TF_SeleccionarMateria.FormActivate(Sender: TObject);
begin
  E_Busqueda.SetFocus;
end;

procedure TF_SeleccionarMateria.DBG_TestKeyPress(Sender: TObject; var Key: Char);
begin
  if (Key = chr(13)) then
    B_SeleccionarClick(nil);
end;

procedure TF_SeleccionarMateria.E_BusquedaKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
    BuscarTest;
end;

procedure TF_SeleccionarMateria.AjustarVentana;
begin
  Panel2.Width:=Trunc(Panel1.Width/2);
  Panel3.Width:=Trunc(Panel1.Width/2);
  FixDBGridColumnsWidth(DBG_Categoria);
  FixDBGridColumnsWidth(DBG_Test);
end;

procedure TF_SeleccionarMateria.FormResize(Sender: TObject);
begin
  AjustarVentana;
end;

procedure TF_SeleccionarMateria.FixDBGridColumnsWidth(const DBGrid: TDBGrid);
var
  i : integer;
  TotWidth : integer;
  VarWidth: INTEGER;
  ResizableColumnCount : integer;
  AColumn : TColumn;
begin
  //total width of all columns before resize
  TotWidth := 0;
  //how many columns need to be auto-resized
  ResizableColumnCount := 0;

  for i := 0 to -1 + DBGrid.Columns.Count do
  begin
    TotWidth := TotWidth + DBGrid.Columns[i].Width;
    if DBGrid.Columns[i].Field.Tag <> 0 then
      Inc(ResizableColumnCount);
  end;

  //add 1px for the column separator line
  if dgColLines in DBGrid.Options then
    TotWidth := TotWidth + DBGrid.Columns.Count;

  //add indicator column width
  if dgIndicator in DBGrid.Options then
    TotWidth := TotWidth + IndicatorWidth;

  //width vale "left"
  VarWidth :=  DBGrid.ClientWidth - TotWidth;

  //Equally distribute VarWidth
  //to all auto-resizable columns
  if ResizableColumnCount > 0 then
    VarWidth := varWidth div ResizableColumnCount;

  for i := 0 to -1 + DBGrid.Columns.Count do
  begin
    AColumn := DBGrid.Columns[i];
    if AColumn.Field.Tag <> 0 then
    begin
      AColumn.Width := AColumn.Width + VarWidth;
      if AColumn.Width < AColumn.Field.Tag then
        AColumn.Width := AColumn.Field.Tag;
    end;
  end;
end; (*FixDBGridColumnsWidth*)


end.
